﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoNetApplication.Common;
using VideoNetApplication.Common.Helper;
using VideoNetApplication.Common.Service;
using VideoNetApplication.Common.Web;

namespace VideoNetApplication.Control
{
    public partial class Comment : System.Web.UI.UserControl
    {
        private string _pageSize = "1";
        //[UserControlRenderingProperty(Key = "PageSize", Source = UserControlRenderingPropertySource.Form)]
        public string PageSize
        {
            get { return this._pageSize; }
            set { this._pageSize = value; }
        }

        /// <summary>
        /// 当前第几页

        /// </summary>
        private string _pageIndex = "1";
        //[UserControlRenderingProperty(Key = "PageIndex", Source = UserControlRenderingPropertySource.Form)]
        public string PageIndex
        {
            get { return this._pageIndex; }
            set { this._pageIndex = value; }
        }


        /// <summary>
        /// 所属项目

        /// </summary>
        private string _pCode = string.Empty;
        //[UserControlRenderingProperty(Key = "PCode", Source = UserControlRenderingPropertySource.Form)]
        public string PCode
        {
            get { return this._pCode; }
            set { this._pCode = value; }
        }


        /// <summary>
        /// ID
        /// </summary>
        private string _itemId = string.Empty;
        //[UserControlRenderingProperty(Key = "ItemID", Source = UserControlRenderingPropertySource.Form)]
        public string ItemID
        {
            get { return this._itemId; }
            set { this._itemId = value; }
        }


        /// <summary>
        /// 用户编号
        /// </summary>
        private string _userCode = string.Empty;
        //[UserControlRenderingProperty(Key = "UserCode", Source = UserControlRenderingPropertySource.Form)]
        public string UserCode
        {
            get { return this._userCode; }
            set { this._userCode = value; }
        }


        /// <summary>
        /// 页面Form参数封装
        /// </summary>
        private MessageArguments _args;

        /// <summary>
        /// 界面显示接口封装
        /// </summary>
        private CommentService _show;


        /// <summary>
        /// 消息实体集合
        /// </summary>
        public List<V_VideoComment> megEntityList = new List<V_VideoComment>();


        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _show = new CommentService();

            this.InitUI();

            RenderMessage(); //初始化留言控件
        }


        private void InitUI()
        {
            this._args = new MessageArguments(this.Page.Request.Form);
            this._args.PageIndex = DataHelper.GetInteger(this.PageIndex);

            if (!this._args.IsReload)  //是否是首次加载
            {
                this.hfPageSize.Value = this.PageSize;
                this._args.PageSize = DataHelper.GetInteger(this.PageSize);
                

                GetPageCount();
            }

            this.hfPCode.Value = this.PCode;
            this.hfItemID.Value = this.ItemID;
            this._args.ItemID = DataHelper.GetInteger(this.ItemID);
            this.hfPageCount.Value = this._args.PageCount.ToString();
            this.PanelDefault.Visible = !this._args.IsReload;
            this.PanelReload.Visible = this._args.IsReload;
        }



        /// <summary>
        /// 获取留言个数
        /// </summary>
        /// <returns></returns>
        private void GetPageCount()
        {
            int pageCount = 0;
            int mesgCnt = 0;
            int result = 0;

            int pageSize = DataHelper.GetInteger(this.PageSize);
            mesgCnt = this._show.GetMessageCnt(this._args);

            pageCount = mesgCnt / pageSize;

            result = mesgCnt % pageSize;

            if (result > 0)
                pageCount = pageCount + 1;

            this._args.PageCount = pageCount;
        }


        /// <summary>
        /// 初始化留言控件
        /// </summary>
        /// <returns></returns>
        public void RenderMessage()
        {
            megEntityList = this._show.GroupMessage(this._args);
            StringBuilder resultHTML = new StringBuilder();

            foreach (var item in megEntityList)
            {

                resultHTML.AppendFormat("<dl> <dt><strong>{0}</strong><span>发表于{1}</span></dt><dd>{2}</dd></dl>",item.CommentUserName,Convert.ToDateTime(item.CreatedTime).ToString("yyyy-MM-dd"),item.CommentContent);

                //resultHTML.Append(string.Format("<div class='wordlist' id='div_{0}'>", item.ID));
                //resultHTML.Append("<ul class='content'>");
                //resultHTML.Append(string.Format("<li><kbd class='zgray'>{0}的评论：</kbd></li>", item.StudentInfo != null ? item.StudentInfo.Name : (item.UserType == 1 ? "【官方】" : "【匿名】")));
                //resultHTML.Append(string.Format("<li>{0}</li>", item.Message));//评论内容
                //if (item.ReplyList.Count == 0)
                //{
                //    resultHTML.Append(string.Format("<li id='li_{0}'><div class='reply' style='display:none;' id='rep_{0}'></div></li>", item.ID));
                //}
                //else
                //{
                //    resultHTML.Append("<li>");
                //    //这里会组合留言回复
                //    resultHTML.Append(string.Format("<div class='reply' id='rep_{0}'>", item.ID));

                //    foreach (var replyItem in item.ReplyList)
                //    {
                //        resultHTML.Append(string.Format("<ul><li><p class='myreply'>{0}的回复</p><p class='time'>{1}</p></li>", replyItem.StudentInfo != null ? replyItem.StudentInfo.Name : (replyItem.UserType == 1 ? "【官方】" : "【匿名】"), DataHelper.GetDataTimeString(replyItem.SendTime, "yyyy-MM-dd HH:mm"), replyItem.ID));
                //        resultHTML.Append(string.Format("<li>{0}</li></ul>\n", replyItem.Message));
                //    }

                //    resultHTML.Append("</div>");
                //    resultHTML.Append("</li>");
                //}

                //if (MembershipContext.User != null)
                //    resultHTML.Append(string.Format("<li><a href='javascript:ReplyComment({0})' target='_self' title='回复'>回复</a></li>", item.ID));

                //resultHTML.Append(string.Format("<div class='replycomment' id='reply_{0}' style='display:none;'></div>", item.ID));
                //resultHTML.Append("</ul>");
                //resultHTML.Append(string.Format("<ul class='time'>{0}</ul>", DataHelper.GetDataTimeString(item.SendTime, "yyyy-MM-dd HH:mm")));
                //resultHTML.Append("</div>");
            }


            //输出留言显示
            if (this._args.IsReload)
            {
                this.literalMsgContentReload.Text = resultHTML.ToString();
            }
            else
            {
                this.literalMsgContent.Text = resultHTML.ToString();
            }
        }
    }
}